import React from 'react';
import {
    ActivityIndicator,
    Button,
    Text,
    View,
    Image,
    StyleSheet,
    Dimensions,
    StatusBar,
    FlatList,
    ScrollView,
    RefreshControl,
    Animated,
    TouchableHighlight,
    SectionList, TouchableOpacity
} from 'react-native';

//引入
import ScrollableTabView, {DefaultTabBar, ScrollableTabBar} from 'react-native-scrollable-tab-view';
import styles from './styles';
import app from "../../../app";
import Httpc from "../../utils/Httpc";
import REQUEST from "../../../REQUEST";
const { width, height } = Dimensions.get('window')



export default class Recommend extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            type:0,
            page:0,
            rows:10,
            total:0,
            totalpage:1,
            lists: [],
            loaded: true,
            refreshing:false,
        };
    }
    static navigationOptions = {
        //title: '彩票咨询',
        //header: null,  //隐藏顶部导航栏
        //tabBarVisible: false, // 隐藏底部导航栏
        headerStyle: {
            backgroundColor: '#ff0000',
        },
        headerTintColor: '#fff',
        headerTitleStyle: {
            //fontWeight: 'bold',
        },
    }
    componentWillMount(){
        this.setState({
            type:this.props.type
        })
    }
    componentDidMount() {
        this.getServerData(()=>{});
    }
    initPage(callback){
        const {type,rows,totalpage} = this.state;
        this.setState({
            type:type,
            page:0,
            rows:rows,
            total:0,
            totalpage:totalpage,
        });
        callback();
    }
    getServerData(callback){
        let _this = this;
        let params = {};
        let type = this.state.type;
        let page = this.state.page;
        let rows = this.state.rows;
        let lists = this.state.lists;
        if(page==0){
            lists = [];
        }
        params.rootid = app.rootid;
        params.type = type;
        params.page = page+1;
        params.rows= rows;
        params.recommend = 0;
        Httpc.get(REQUEST.News,params,function (res) {            
            let newlists = lists.concat(res.rows);
            _this.setState({
                type:type,
                page:page+1,
                rows:rows,
                total:res.total,
                totalpage:res.totalpage,
                lists:newlists,
            });
            callback();
        })
    }
    _on_newsDeatiled=(id)=>{
        this.props.navigation.navigate('NewsDetailed',{
            id:id
        });
    }
    _createNewsItem=({item})=>{
        return (
            <TouchableOpacity  onPress={() => this._on_newsDeatiled(item.id)}>
            <View style={styles.recommend_lists}>
                <View style={styles.recommend_lists_title}>
                    <View style={{alignItems:'flex-start',height:65}}>
                        <Text style={{fontSize:14}}>{item.title}</Text>
                    </View>
                    <View style={styles.cate}>
                        <Text style={{fontSize:12,color:'#999999',width:65,height:14,overflow:'hidden'}}>{item.Ctime}</Text>
                        <Text style={{fontSize:12,color:'#999999',width:65,height:14,overflow:'hidden'}}></Text>
                    </View>
                </View>
                <Image style={styles.thumb} source={{uri:item.headimage}} />
            </View>
            </TouchableOpacity>
        );
    }
    _onRefresh = () => {
        if(this.state.refreshing){
            return;
        }
        this.setState({refreshing: true});
        let _this = this;
        let params = {};
        let type = this.state.type;
        let rows = this.state.rows;
        params.rootid = app.rootid;
        params.type = type;
        params.page = 1;
        params.rows= rows;
        params.recommend = 0;
        Httpc.get(REQUEST.News,params,function (res) {
            _this.setState({
                type:type,
                page:1,
                rows:rows,
                total:res.total,
                totalpage:res.totalpage,
                lists:res.rows,
            });
            _this.setState({refreshing: false});
        })
    }
    _onEndReached(){
        if(this.state.page < this.state.totalpage){
            this.getServerData(()=>{});
        }else{
            console.log('我是有底线的');
        }
    }
    _separator(){
        return <View style={{height:0.5,backgroundColor:'#ededed'}}/>;
    }
    render() {
        let {lists} = this.state;
        return (
            <View style={styles.container}>
                <FlatList
                    refreshControl={
                        <RefreshControl
                            refreshing={this.state.refreshing}
                            onRefresh={this._onRefresh}
                        />
                    }
                    data={lists}
                    keyExtractor={(item, index) => index.toString()}
                    renderItem={this._createNewsItem}
                    onEndReached={this._onEndReached.bind(this)}
                    onEndReachedThreshold={1}
                    ItemSeparatorComponent={this._separator}
                />
            </View>
        )
    }



}
